const _ = require('lodash')
const moment = require('moment')

const config = require('../../common/config');
//登录接口
const utils = require('../../utils/index');

const classModel = require('../../models/class');
const combineTimestampsLodash = utils.combineTimestampsLodash;
const formatTimestamp = utils.formatTimestamp;
const millisecondsToTime = utils.millisecondsToTime;
async function classStatus() {
  try {
    const today = moment().utc().valueOf();
    const param = {
      status: [1, 2],
      date: today,
      limit: 10000
    };
    const {
      records: list
    } = await classModel.list(param);

    const editList = _.chain(list)
      .map(item => {
        const [startTime, endTime] = item.classTime;
        const startDateTime = combineTimestampsLodash(formatTimestamp(item.classDate) + " " + millisecondsToTime(startTime));
        const endDateTime = combineTimestampsLodash(formatTimestamp(item.classDate) + " " + millisecondsToTime(endTime));
        console.log(startDateTime);
        console.log(endDateTime);
        console.log(formatTimestamp(startDateTime,'YYYY-MM-DD HH:mm:ss'));
        console.log(formatTimestamp(endDateTime,'YYYY-MM-DD HH:mm:ss'));
        console.log(formatTimestamp(today,'YYYY-MM-DD HH:mm:ss'));
        item.status = endDateTime < today ? 3 :
          startDateTime <= today && endDateTime >= today ? 2 :
          startDateTime > today ? 1 :
          item.status;

        return item;
      })
      .filter(item => item.status !== 1)
      .value();
    return Promise.all(editList.map(item => classModel.edit(item)));
  } catch (error) {
    console.log(error);
  }
}

module.exports = {
  classStatus
};